package com.lky.service;

import com.lky.bean.Employee;
import com.lky.bean.EmployeeExample;
import com.lky.bean.Msg;
import com.lky.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: EmployeeService
 * @Author: Li_Kangyi
 * @Date: 2021/2/23 15:07
 * @Description: TODO
 */
@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

//    查询所有员工：
    public List<Employee> getAll(){
        return employeeMapper.selectByExampleWithDept(null);
    }

//    保存员工：
    public void saveEmp(Employee employee) {
        employeeMapper.insert(employee);
    }

//    检验用户名是否重复：
    public boolean checkuser(String empName) {
        //查询条件容器：
        EmployeeExample example = new EmployeeExample();
        //创造查询条件：
        EmployeeExample.Criteria criteria = example.createCriteria();
        //查询条件为:员工姓名与传入的名字相等的记录：
        criteria.andEmpNameEqualTo(empName);

        //按照传入的条件查询是否有符合条件的数据，有的话返回大于0的数字！
        long count = employeeMapper.countByExample(example);
        //返回true代表没有重复！
        return count == 0;
    }

    public Employee getEmp(Integer id) {
        //按照主键查询员工，也就是按照ID查询：
        Employee employee = employeeMapper.selectByPrimaryKeyWithDept(id);
        return employee;
    }

//    员工更新：
    public void updateEmp(Employee employee) {
        //按照主键有选择的更新员工数据：
        employeeMapper.updateByPrimaryKeySelective(employee);
    }
//    删除员工：
    public void deleteEmpById(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Integer> ids) {
        //创造条件：
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        //拼接SQL为:  delete from xxx where emp_id in(1,2,3)
        criteria.andEmpIdIn(ids);
        //按条件删除：
        employeeMapper.deleteByExample(example);
    }
}
